package com.user.service.impl;
import com.user.model.entity.ServerChuZu;
import com.user.model.entity.ServerInfo;
import com.user.model.entity.ServerZuLin;
import com.user.model.vo.ParamVo;
import com.user.model.vo.QueryServerInfoVo;
import com.user.model.vo.ServerChuZuVo;
import com.user.model.vo.ZulinListVo;
import com.user.param.Result;
import com.user.repositories.OtherRepository;
import com.user.repositories.ServerChuZuRepository;
import com.user.repositories.ServerInfoRepository;
import com.user.service.ServerCZService;
import com.user.util.ChangeUtil;
import com.user.util.GetUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class ServerCZServiceImpl implements ServerCZService {

    @Autowired
    ServerInfoRepository serverInfoRepository;

    @Autowired
    ServerChuZuRepository serverChuZuRepository;

    @Resource
    OtherRepository otherRepository;

    @Override
    @Transactional
    public Result serverChuzu(ServerChuZuVo serverChuZuVo) {

        //先存入serverInfo 信息表
        ServerInfo serverInfo = new ServerInfo();
        ChangeUtil.transalte(serverChuZuVo,serverInfo);
        serverInfo = serverInfoRepository.save(serverInfo);

        if (null == serverInfo){
            return new Result(Result.ERROR_CODE,"存入失败",null);
        }

        //存入出租表
        ServerChuZu serverChuZu = new ServerChuZu();
        serverChuZu.setServerId(serverInfo.getId());
        serverChuZu.setStarTime(serverChuZuVo.getStarTime());
        serverChuZu.setEndTime(serverChuZuVo.getEndTime());
        serverChuZu.setState('N');
        serverChuZu.setMoney(serverChuZuVo.getMoney());

        serverChuZu.setUserId(GetUserUtil.loginUser().getId());
        // serverChuZu.setUserId("4028468170c8642a0170c8684fc40000");
        serverChuZu = serverChuZuRepository.save(serverChuZu);
        if (null == serverChuZu){
            return new Result(Result.ERROR_CODE);
        }
        return new Result(Result.SUCCESS_CODE);
    }

    /**
     *  查询服务器信息列表
     * @param queryServerInfoVo
     * @return
     */
    @Override
    public Result getServerList(QueryServerInfoVo queryServerInfoVo) {

        int count = otherRepository.getServerInfoListCount(queryServerInfoVo);


        if (queryServerInfoVo.getPageIndex()<=1){
            queryServerInfoVo.setPageIndex(1);
        }

        List<ServerChuZuVo> serverChuZuVoList= otherRepository.getServerInfoListByPage(queryServerInfoVo);

        ParamVo paramVo = new ParamVo();
        paramVo.setCount(count);
        paramVo.setObjects(serverChuZuVoList);

        return new Result(Result.SUCCESS_CODE,"请求成功",paramVo);
    }

    @Override
    public Result getServerBeCZ() {
        log.info("=======收益信息接口=======\n");
        String userId = GetUserUtil.loginUser().getId();
        List<ServerChuZu> serverChuZus = serverChuZuRepository.findAllByUserIdAndStateAndDeleted(userId,'Y','1');

        List<ZulinListVo> list = new ArrayList<ZulinListVo>();
        for (ServerChuZu serverChuZu : serverChuZus) {
            ZulinListVo zulinListVo = new ZulinListVo();
            zulinListVo.setServerChuZu(serverChuZu);
            zulinListVo.setServerInfo(serverInfoRepository.findByIdAndDeleted(serverChuZu.getServerId(), '1'));
            list.add(zulinListVo);
        }


        return new Result(list);
    }
}
